package com.extension.company.model.my;

import android.app.Application;
import android.util.Log;

import com.extension.company.common.widget.dialog.LoadingDialog;
import com.extension.company.contract.AddressListAddContract;
import com.extension.company.common.http.ExceptionHandle;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.AddressSaveData;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class AddressListAddMode implements AddressListAddContract.Mode {

	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);

	/**
	 * @param map      新增收货地址的请求体
	 * @param callback 请求过后的回调
	 */
	@Override
	public void saveAddressRequest(HashMap<String, String> map, final AddressListAddContract.RequestCallback callback) {

		retrofitUrlApi.postAddAddress(map)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<AddressSaveData>() {
					@Override
					public void onSubscribe(Disposable d) {
						callback.showLoading();
					}

					@Override
					public void onNext(AddressSaveData addressSaveData) {
						if (200 == addressSaveData.getCode()) {
							callback.callbackSuccss(addressSaveData.getMsg());
						}
					}

					@Override
					public void onError(Throwable e) {
						ExceptionHandle.ResponeThrowable exception = ExceptionHandle.handleException(e);
						callback.callbackFaile(exception.message);
					}

					@Override
					public void onComplete() {

					}
				});
	}

	@Override
	public void editAddressRequest(HashMap<String, String> map, final AddressListAddContract.RequestCallback callback) {

		retrofitUrlApi.postEditAddress(map)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<AddressSaveData>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(AddressSaveData addressSaveData) {
						if (200 == addressSaveData.getCode()) {
							callback.callbackSuccss(addressSaveData.getMsg());
						}
					}

					@Override
					public void onError(Throwable e) {
						ExceptionHandle.ResponeThrowable exception = ExceptionHandle.handleException(e);
						callback.callbackFaile(exception.message);
					}

					@Override
					public void onComplete() {

					}
				});

	}
}
